package com.fw.ui.view.profile;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import com.fw.service.bean.EntryBean;
import com.fw.service.bean.SearchCriteriaBean;
import com.fw.service.bean.SearchResultBean;
import com.fw.service.merchantprofile.bean.MerchantProfileBean;
import com.fw.service.merchantprofile.bean.SearchMerchantProfileBean;
import com.fw.ui.bao.MasterDataBAO;
import com.fw.ui.bao.MerchantProfileBAO;
import com.fw.ui.bao.MessageBAO;
import com.fw.ui.bao.PropertyBAO;
import com.fw.ui.bean.merchantprofile.SearchMerchantProfileTableItemBean;
import com.fw.ui.core.ViewDescription;
import com.fw.ui.util.VaadinApplicationObjectSupport;
import com.fw.ui.view.AbstractView;
import com.fw.ui.view.common.SortingTablePanel;
import com.fw.ui.view.common.TablePanel;
import com.fw.ui.view.common.util.FileStreamSource;
import com.fw.ui.view.common.util.TableHeaderProperty;
import com.fw.ui.view.profile.merchant.event.SearchMerchantProfileClearButtonClickListener;
import com.fw.ui.view.profile.merchant.event.SearchMerchantProfileExportButtonClickListener;
import com.fw.ui.view.profile.merchant.event.SearchMerchantProfileNewButtonClickListener;
import com.fw.ui.view.profile.merchant.event.SearchMerchantProfileSearchButtonClickListener;
import com.fw.ui.view.profile.merchant.event.SearchMerchantProfileTableDeleteButtonClickListener;
import com.fw.ui.view.profile.merchant.event.SearchMerchantProfileTableEditButtonClickListener;
import com.fw.ui.view.profile.merchant.event.SearchMerchantProfileTablePageButtonClickListener;
import com.fw.ui.view.profile.merchant.event.SearchMerchantProfileTableSizeValueChangeListener;
import com.fw.ui.view.profile.merchant.event.SearchMerchantProfileTableSortButtonClickListener;
import com.fw.ui.view.profile.merchant.event.SearchMerchantProfileTableViewButtonClickListener;
import com.fw.util.StringUtils;
import com.vaadin.data.util.BeanItem;
import com.vaadin.server.StreamResource;
import com.vaadin.shared.ui.combobox.FilteringMode;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table.Align;
import com.vaadin.ui.VerticalLayout;

@org.springframework.stereotype.Component
@Scope(SCOPE_PROTOTYPE)
@ViewDescription(name = "profile-merchant-search", path = "/profile/merchant/search")
public class SearchMerchantProfileView extends AbstractView {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Autowired
    protected transient VaadinApplicationObjectSupport app;

    @Autowired
    private MasterDataBAO masterDataBAO;
    @Autowired
    private MessageBAO messageBAO;
    @Autowired
    private PropertyBAO propertyBAO;
    @Autowired
    private MerchantProfileBAO merchantProfileBAO;

    private Map<String, String> statusMap = new HashMap<String, String>();
    private Map<String, String> merchantServiceTypeMap = new HashMap<String, String>();
    private SearchMerchantProfileBean searchMerchantProfileBean = new SearchMerchantProfileBean();
    private SearchCriteriaBean<SearchMerchantProfileBean> searchCriteriaBean = new SearchCriteriaBean<SearchMerchantProfileBean>();

    @Autowired
    private SearchMerchantProfileExportButtonClickListener searchMerchantProfileExportButtonClickListener;
    @Autowired
    private SearchMerchantProfileClearButtonClickListener searchMerchantProfileClearButtonClickListener;
    @Autowired
    private SearchMerchantProfileNewButtonClickListener searchMerchantProfileNewButtonClickListener;
    @Autowired
    private SearchMerchantProfileSearchButtonClickListener searchMerchantProfileSearchButtonClickListener;
    @Autowired
    private SearchMerchantProfileTableDeleteButtonClickListener searchMerchantProfileTableDeleteButtonClickListener;
    @Autowired
    private SearchMerchantProfileTableEditButtonClickListener searchMerchantProfileTableEditButtonClickListener;
    @Autowired
    private SearchMerchantProfileTablePageButtonClickListener searchMerchantProfileTablePageButtonClickListener;
    @Autowired
    private SearchMerchantProfileTableSizeValueChangeListener searchMerchantProfileTableSizeValueChangeListener;
    @Autowired
    private SearchMerchantProfileTableSortButtonClickListener searchMerchantProfileTableSortButtonClickListener;
    @Autowired
    private SearchMerchantProfileTableViewButtonClickListener searchMerchantProfileTableViewButtonClickListener;

    /**
     * COMPONENT
     */
    private Panel searchMerchantProfilePanel = new Panel();

    @Autowired
    protected TablePanel tablePanel;
    @Autowired
    protected SortingTablePanel sortingTablePanel;

    private Label lblMerchantId = new Label();
    private Label lblCompanyName = new Label();
    private Label lblMerchantUrl = new Label();
    private Label lblProjectContactPerson = new Label();
    private Label lblMerchantServiceType = new Label();
    private Label lblStatus = new Label();

    private ComboBox cbbMerchantServiceType = new ComboBox();
    private ComboBox cbbStatus = new ComboBox();
    private ComboBox cbbMerchantId = new ComboBox();
    private ComboBox cbbCompanyName = new ComboBox();
    private ComboBox cbbMerchantUrl = new ComboBox();
    private ComboBox cbbProjectContactPerson = new ComboBox();
    private CheckBox cbHideMerchantTest = new CheckBox();

    private Button btnSearch = new Button();
    private Button btnNew = new Button();
    private Button btnClear = new Button();
    private Button btnExport = new Button();

    private TableHeaderProperty hNo = new TableHeaderProperty();
    private TableHeaderProperty hMerchantId = new TableHeaderProperty();
    private TableHeaderProperty hMerchantName = new TableHeaderProperty();
    private TableHeaderProperty hCompanyName = new TableHeaderProperty();
    private TableHeaderProperty hMerchantUrl = new TableHeaderProperty();
    private TableHeaderProperty hMerchantServiceType = new TableHeaderProperty();
    private TableHeaderProperty hProjectContactPerson = new TableHeaderProperty();
    private TableHeaderProperty hStatus = new TableHeaderProperty();
    private TableHeaderProperty hCreateDate = new TableHeaderProperty();
    private TableHeaderProperty hLastUpdateDate = new TableHeaderProperty();
    private TableHeaderProperty hCommand = new TableHeaderProperty();

    private List<Button> tableViewButtons = new ArrayList<Button>();
    private List<Button> tableEditButtons = new ArrayList<Button>();
    private List<Button> tableDeleteButtons = new ArrayList<Button>();

    @Override
    public void updateStrings() {
	
	searchMerchantProfilePanel.setCaption(this.messageBAO.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.title"));

	this.lblMerchantId.setValue(this.messageBAO.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.label.merchantId"));
	this.lblCompanyName.setValue(this.messageBAO.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.label.companyName"));
	this.lblMerchantUrl.setValue(this.messageBAO.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.label.merchantUrl"));
	this.lblProjectContactPerson.setValue(this.messageBAO
		.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.label.projectContactPerson"));
	this.lblMerchantServiceType.setValue(this.messageBAO
		.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.label.merchantServiceType"));
	this.lblStatus.setValue(this.messageBAO.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.label.status"));
	this.cbHideMerchantTest.setCaption(this.messageBAO
		.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.label.hideMerchantTest"));

	this.cbbMerchantId.setInputPrompt(this.messageBAO
		.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.inputprompt.merchantId"));
	this.cbbCompanyName.setInputPrompt(this.messageBAO
		.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.inputprompt.companyName"));
	this.cbbMerchantUrl.setInputPrompt(this.messageBAO
		.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.inputprompt.merchantUrl"));
	this.cbbProjectContactPerson.setInputPrompt(this.messageBAO
		.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.inputprompt.projectContactPerson"));
	this.cbbMerchantServiceType.setInputPrompt(this.messageBAO
		.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.inputprompt.merchantServiceType"));
	this.cbbStatus.setInputPrompt(this.messageBAO.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.inputprompt.status"));

	this.btnSearch.setCaption(this.messageBAO.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.button.search"));
	this.btnNew.setCaption(this.messageBAO.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.button.new"));
	this.btnClear.setCaption(this.messageBAO.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.button.clear"));
	this.btnExport.setCaption(this.messageBAO.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.button.export"));

	this.hNo.setColumnName(this.messageBAO.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.header.no"));
	this.hMerchantId.setColumnName(this.messageBAO.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.header.merchantId"));
	this.hMerchantName
		.setColumnName(this.messageBAO.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.header.merchantName"));
	this.hCompanyName.setColumnName(this.messageBAO.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.header.companyName"));
	this.hMerchantUrl.setColumnName(this.messageBAO.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.header.merchantUrl"));
	this.hMerchantServiceType.setColumnName(this.messageBAO
		.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.header.merchantServiceType"));
	this.hProjectContactPerson.setColumnName(this.messageBAO
		.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.header.projectContactPerson"));
	this.hStatus.setColumnName(this.messageBAO.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.header.status"));
	this.hCreateDate
		.setColumnName(this.messageBAO.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.header.createDateTime"));
	this.hLastUpdateDate.setColumnName(this.messageBAO
		.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.header.lastUpdateDateTime"));
	this.hCommand.setColumnName(this.messageBAO.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.header.command"));

	tablePanel.setTableColumnHeader(this.hNo.getColumnId(), this.hNo.getColumnName());
	tablePanel.setTableColumnHeader(this.hMerchantId.getColumnId(), this.hMerchantId.getColumnName());
	tablePanel.setTableColumnHeader(this.hMerchantName.getColumnId(), this.hMerchantName.getColumnName());
	tablePanel.setTableColumnHeader(this.hCompanyName.getColumnId(), this.hCompanyName.getColumnName());
	tablePanel.setTableColumnHeader(this.hMerchantUrl.getColumnId(), this.hMerchantUrl.getColumnName());
	tablePanel.setTableColumnHeader(this.hMerchantServiceType.getColumnId(), this.hMerchantServiceType.getColumnName());
	tablePanel.setTableColumnHeader(this.hProjectContactPerson.getColumnId(), this.hProjectContactPerson.getColumnName());
	tablePanel.setTableColumnHeader(this.hStatus.getColumnId(), this.hStatus.getColumnName());
	tablePanel.setTableColumnHeader(this.hCreateDate.getColumnId(), this.hCreateDate.getColumnName());
	tablePanel.setTableColumnHeader(this.hLastUpdateDate.getColumnId(), this.hLastUpdateDate.getColumnName());
	tablePanel.setTableColumnHeader(this.hCommand.getColumnId(), this.hCommand.getColumnName());

	if (tableViewButtons != null && !tableViewButtons.isEmpty()) {
	    for (Button button : tableViewButtons) {
		button.setCaption(this.messageBAO.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.button.table.view"));
	    }
	}

	if (tableEditButtons != null && !tableEditButtons.isEmpty()) {
	    for (Button button : tableEditButtons) {
		button.setCaption(this.messageBAO.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.button.table.edit"));
	    }
	}

	if (tableDeleteButtons != null && !tableDeleteButtons.isEmpty()) {
	    for (Button button : tableDeleteButtons) {
		button.setCaption(this.messageBAO.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.button.table.delete"));
	    }
	}

    }

    @Override
    public void init() {
	this.setSizeFull();

	this.searchMerchantProfilePanel.setSizeFull();
	this.searchMerchantProfilePanel.setId("searchMerchantProfilePanel");

	lblMerchantId.setWidth("150px");
	lblCompanyName.setWidth("150px");
	lblProjectContactPerson.setWidth("150px");
	lblMerchantServiceType.setWidth("150px");
	lblMerchantUrl.setWidth("100px");
	lblStatus.setWidth("100px");

	cbbMerchantId.setWidth("150px");
	cbbCompanyName.setWidth("150px");
	cbbMerchantUrl.setWidth("150px");
	cbbProjectContactPerson.setWidth("150px");
	cbbMerchantServiceType.setWidth("150px");
	cbbStatus.setWidth("150px");

	this.cbbMerchantId.setFilteringMode(FilteringMode.STARTSWITH);
	this.cbbCompanyName.setFilteringMode(FilteringMode.STARTSWITH);
	this.cbbMerchantUrl.setFilteringMode(FilteringMode.STARTSWITH);
	this.cbbProjectContactPerson.setFilteringMode(FilteringMode.STARTSWITH);

	this.cbbMerchantId.setNewItemsAllowed(true);
	this.cbbCompanyName.setNewItemsAllowed(true);
	this.cbbMerchantUrl.setNewItemsAllowed(true);
	this.cbbProjectContactPerson.setNewItemsAllowed(true);

	this.cbbMerchantServiceType.setNullSelectionAllowed(false);
	this.cbbStatus.setNullSelectionAllowed(false);
	this.cbbMerchantServiceType.setTextInputAllowed(false);
	this.cbbStatus.setTextInputAllowed(false);

	this.btnSearch.addStyleName("button-blue");
	this.btnNew.addStyleName("button-blue");
	this.btnExport.addStyleName("button-blue");
	this.btnClear.addStyleName("button-green");

	this.btnSearch.setDisableOnClick(true);
	this.btnNew.setDisableOnClick(true);
	this.btnExport.setDisableOnClick(true);
	this.btnClear.setDisableOnClick(true);

	this.btnSearch.setWidth("70px");
	this.btnNew.setWidth("70px");
	this.btnExport.setWidth("70px");
	this.btnClear.setWidth("70px");

	this.hNo.setColumnId("seqNo");
	this.hNo.setType(Integer.class);
	this.hNo.setAlignment(Align.CENTER);
	this.hNo.setDefaultValue(null);
	this.hNo.setIconResource(null);
	this.hNo.setWidth(30);
	this.hNo.setOrder(1);

	this.hMerchantId.setColumnId("merchantId");
	this.hMerchantId.setType(String.class);
	this.hMerchantId.setAlignment(Align.CENTER);
	this.hMerchantId.setDefaultValue(null);
	this.hMerchantId.setIconResource(null);
	this.hMerchantId.setWidth(80);
	this.hMerchantId.setOrder(2);

	this.hMerchantName.setColumnId("merchantName");
	this.hMerchantName.setType(String.class);
	this.hMerchantName.setAlignment(Align.LEFT);
	this.hMerchantName.setDefaultValue(null);
	this.hMerchantName.setIconResource(null);
	this.hMerchantName.setOrder(3);

	this.hCompanyName.setColumnId("companyName");
	this.hCompanyName.setType(String.class);
	this.hCompanyName.setAlignment(Align.LEFT);
	this.hCompanyName.setDefaultValue(null);
	this.hCompanyName.setIconResource(null);
	this.hCompanyName.setOrder(4);

	this.hMerchantUrl.setColumnId("merchantUrl");
	this.hMerchantUrl.setType(String.class);
	this.hMerchantUrl.setAlignment(Align.CENTER);
	this.hMerchantUrl.setDefaultValue(null);
	this.hMerchantUrl.setIconResource(null);
	this.hMerchantUrl.setWidth(120);
	this.hMerchantUrl.setOrder(5);

	this.hMerchantServiceType.setColumnId("merchantServiceType");
	this.hMerchantServiceType.setType(String.class);
	this.hMerchantServiceType.setAlignment(Align.CENTER);
	this.hMerchantServiceType.setDefaultValue(null);
	this.hMerchantServiceType.setIconResource(null);
	this.hMerchantServiceType.setWidth(130);
	this.hMerchantServiceType.setOrder(6);

	this.hProjectContactPerson.setColumnId("projectContactPerson");
	this.hProjectContactPerson.setType(String.class);
	this.hProjectContactPerson.setAlignment(Align.CENTER);
	this.hProjectContactPerson.setDefaultValue(null);
	this.hProjectContactPerson.setIconResource(null);
	this.hProjectContactPerson.setWidth(130);
	this.hProjectContactPerson.setOrder(7);

	this.hStatus.setColumnId("status");
	this.hStatus.setType(String.class);
	this.hStatus.setAlignment(Align.CENTER);
	this.hStatus.setDefaultValue(null);
	this.hStatus.setIconResource(null);
	this.hStatus.setWidth(80);
	this.hStatus.setOrder(8);

	this.hCreateDate.setColumnId("createDateTime");
	this.hCreateDate.setType(String.class);
	this.hCreateDate.setAlignment(Align.CENTER);
	this.hCreateDate.setDefaultValue(null);
	this.hCreateDate.setIconResource(null);
	this.hCreateDate.setWidth(110);
	this.hCreateDate.setOrder(9);

	this.hLastUpdateDate.setColumnId("lastUpdateDateTime");
	this.hLastUpdateDate.setType(String.class);
	this.hLastUpdateDate.setAlignment(Align.CENTER);
	this.hLastUpdateDate.setDefaultValue(null);
	this.hLastUpdateDate.setIconResource(null);
	this.hLastUpdateDate.setWidth(130);
	this.hLastUpdateDate.setOrder(10);

	this.hCommand.setColumnId("commandLayout");
	this.hCommand.setType(HorizontalLayout.class);
	this.hCommand.setAlignment(Align.CENTER);
	this.hCommand.setDefaultValue(null);
	this.hCommand.setIconResource(null);
	this.hCommand.setWidth(140);
	this.hCommand.setOrder(12);

	List<TableHeaderProperty> headerProperties = new ArrayList<TableHeaderProperty>();
	headerProperties.add(hNo);
	headerProperties.add(hMerchantId);
	headerProperties.add(hMerchantName);
	headerProperties.add(hCompanyName);
	headerProperties.add(hMerchantUrl);
	headerProperties.add(hMerchantServiceType);
	headerProperties.add(hProjectContactPerson);
	headerProperties.add(hStatus);
	headerProperties.add(hCreateDate);
	headerProperties.add(hLastUpdateDate);
	headerProperties.add(hCommand);

	this.tablePanel.setRowIndexVisiblePageNumber();
	this.tablePanel.setTableSelect(false);
	this.tablePanel.setAllowSortRecord(false);
	this.tablePanel.setEnableTableSorting(false);
	this.tablePanel.setTableColumnReordering(false);
	this.tablePanel.createControlTablePageNumber();
	this.tablePanel.setSortingTablePanel(this.sortingTablePanel);
	this.tablePanel.setTableBeanContainerColumnHeader(headerProperties, SearchMerchantProfileTableItemBean.class);

	VerticalLayout mainLayout = new VerticalLayout();
	mainLayout.setSizeFull();
	mainLayout.setSizeUndefined();
	mainLayout.setSpacing(true);
	mainLayout.setWidth("100%");

	this.searchMerchantProfilePanel.setContent(mainLayout);

	VerticalLayout formLayout = new VerticalLayout();
	formLayout.setId("formPanel");
	formLayout.setSizeUndefined();
	formLayout.setWidth("100%");
	formLayout.setSpacing(true);

	HorizontalLayout row0 = new HorizontalLayout();
	HorizontalLayout row1 = new HorizontalLayout();
	HorizontalLayout row2 = new HorizontalLayout();
	HorizontalLayout row3 = new HorizontalLayout();
	HorizontalLayout row4 = new HorizontalLayout();

	row1.setSpacing(true);
	row2.setSpacing(true);
	row3.setSpacing(true);
	row4.setSpacing(true);

	row1.setSizeUndefined();
	row2.setSizeUndefined();
	row3.setSizeUndefined();
	row4.setSizeUndefined();

	HorizontalLayout row1Col1 = new HorizontalLayout();
	HorizontalLayout row1Sep1 = new HorizontalLayout();
	HorizontalLayout row1Col2 = new HorizontalLayout();
	HorizontalLayout row1Sep2 = new HorizontalLayout();
	HorizontalLayout row1Col3 = new HorizontalLayout();
	HorizontalLayout row1Sep3 = new HorizontalLayout();
	HorizontalLayout row1Col4 = new HorizontalLayout();

	HorizontalLayout row2Col1 = new HorizontalLayout();
	HorizontalLayout row2Sep1 = new HorizontalLayout();
	HorizontalLayout row2Col2 = new HorizontalLayout();
	HorizontalLayout row2Sep2 = new HorizontalLayout();
	HorizontalLayout row2Col3 = new HorizontalLayout();
	HorizontalLayout row2Sep3 = new HorizontalLayout();
	HorizontalLayout row2Col4 = new HorizontalLayout();

	row1Col4.setWidth("150px");
	row2Col4.setWidth("150px");

	row1Col1.setSpacing(true);
	row1Col2.setSpacing(true);
	row1Col3.setSpacing(true);
	row1Col4.setSpacing(true);

	row2Col1.setSpacing(true);
	row2Col2.setSpacing(true);
	row2Col3.setSpacing(true);
	row2Col4.setSpacing(true);

	row1Col1.addComponent(lblMerchantId);
	row1Col1.addComponent(cbbMerchantId);
	row1Col2.addComponent(lblCompanyName);
	row1Col2.addComponent(cbbCompanyName);
	row1Col3.addComponent(lblMerchantUrl);
	row1Col3.addComponent(cbbMerchantUrl);
	row1Col4.addComponent(cbHideMerchantTest);

	row1Col1.setComponentAlignment(lblMerchantId, Alignment.MIDDLE_LEFT);
	row1Col1.setComponentAlignment(cbbMerchantId, Alignment.MIDDLE_LEFT);
	row1Col2.setComponentAlignment(lblCompanyName, Alignment.MIDDLE_LEFT);
	row1Col2.setComponentAlignment(cbbCompanyName, Alignment.MIDDLE_LEFT);
	row1Col3.setComponentAlignment(lblMerchantUrl, Alignment.MIDDLE_LEFT);
	row1Col3.setComponentAlignment(cbbMerchantUrl, Alignment.MIDDLE_LEFT);
	row1Col4.setComponentAlignment(cbHideMerchantTest, Alignment.MIDDLE_LEFT);

	row2Col1.addComponent(lblProjectContactPerson);
	row2Col1.addComponent(cbbProjectContactPerson);
	row2Col2.addComponent(lblMerchantServiceType);
	row2Col2.addComponent(cbbMerchantServiceType);
	row2Col3.addComponent(lblStatus);
	row2Col3.addComponent(cbbStatus);

	row2Col1.setComponentAlignment(lblProjectContactPerson, Alignment.MIDDLE_LEFT);
	row2Col1.setComponentAlignment(cbbProjectContactPerson, Alignment.MIDDLE_LEFT);
	row2Col2.setComponentAlignment(lblMerchantServiceType, Alignment.MIDDLE_LEFT);
	row2Col2.setComponentAlignment(cbbMerchantServiceType, Alignment.MIDDLE_LEFT);
	row2Col3.setComponentAlignment(lblStatus, Alignment.MIDDLE_LEFT);
	row2Col3.setComponentAlignment(cbbStatus, Alignment.MIDDLE_LEFT);

	row1.addComponent(row1Col1);
	row1.addComponent(row1Sep1);
	row1.addComponent(row1Col2);
	row1.addComponent(row1Sep2);
	row1.addComponent(row1Col3);
	row1.addComponent(row1Sep3);
	row1.addComponent(row1Col4);

	row2.addComponent(row2Col1);
	row2.addComponent(row2Sep1);
	row2.addComponent(row2Col2);
	row2.addComponent(row2Sep2);
	row2.addComponent(row2Col3);
	row2.addComponent(row2Sep3);
	row2.addComponent(row2Col4);

	row4.addComponent(btnSearch);
	row4.addComponent(btnNew);
	row4.addComponent(btnExport);
	row4.addComponent(btnClear);

	formLayout.addComponent(row0);
	formLayout.addComponent(row1);
	formLayout.addComponent(row2);
	formLayout.addComponent(row3);
	formLayout.addComponent(row4);

	formLayout.setComponentAlignment(row1, Alignment.MIDDLE_CENTER);
	formLayout.setComponentAlignment(row2, Alignment.MIDDLE_CENTER);
	formLayout.setComponentAlignment(row3, Alignment.MIDDLE_CENTER);
	formLayout.setComponentAlignment(row4, Alignment.MIDDLE_CENTER);

	HorizontalLayout tableSortingLayout = new HorizontalLayout();
	tableSortingLayout.setSizeUndefined();
	tableSortingLayout.setWidth("99%");
	tableSortingLayout.addComponent(this.sortingTablePanel);

	HorizontalLayout tableLayout = new HorizontalLayout();
	tableLayout.setSizeUndefined();
	tableLayout.setWidth("99%");
	tableLayout.addComponent(this.tablePanel);

	mainLayout.addComponent(formLayout);
	mainLayout.addComponent(tableSortingLayout);
	mainLayout.addComponent(tableLayout);
	mainLayout.setExpandRatio(tableLayout, 1);

	mainLayout.setComponentAlignment(formLayout, Alignment.MIDDLE_CENTER);
	mainLayout.setComponentAlignment(tableSortingLayout, Alignment.MIDDLE_CENTER);
	mainLayout.setComponentAlignment(tableLayout, Alignment.MIDDLE_CENTER);

	this.addComponent(this.searchMerchantProfilePanel);
	this.setComponentAlignment(this.searchMerchantProfilePanel, Alignment.TOP_CENTER);

    }

    @Override
    public void initListener() {
	this.searchMerchantProfileExportButtonClickListener.setBasedComponent(this);
	this.searchMerchantProfileClearButtonClickListener.setBasedComponent(this);
	this.searchMerchantProfileNewButtonClickListener.setBasedComponent(this);
	this.searchMerchantProfileSearchButtonClickListener.setBasedComponent(this);
	this.searchMerchantProfileTableDeleteButtonClickListener.setBasedComponent(this);
	this.searchMerchantProfileTableEditButtonClickListener.setBasedComponent(this);
	this.searchMerchantProfileTablePageButtonClickListener.setBasedComponent(this);
	this.searchMerchantProfileTableSizeValueChangeListener.setBasedComponent(this);
	this.searchMerchantProfileTableSortButtonClickListener.setBasedComponent(this);
	this.searchMerchantProfileTableViewButtonClickListener.setBasedComponent(this);

	this.searchMerchantProfileExportButtonClickListener.setAutoEnabled(true);
	this.searchMerchantProfileClearButtonClickListener.setAutoEnabled(true);
	this.searchMerchantProfileNewButtonClickListener.setAutoEnabled(true);
	this.searchMerchantProfileSearchButtonClickListener.setAutoEnabled(true);
	this.searchMerchantProfileTableViewButtonClickListener.setAutoEnabled(true);
	this.searchMerchantProfileTableDeleteButtonClickListener.setAutoEnabled(true);
	this.searchMerchantProfileTableEditButtonClickListener.setAutoEnabled(true);

	this.searchMerchantProfileTablePageButtonClickListener.setTablePanel(this.tablePanel);
	this.searchMerchantProfileTableSizeValueChangeListener.setTablePanel(this.tablePanel);
	this.searchMerchantProfileTableSortButtonClickListener.setTablePanel(this.tablePanel);

	this.tablePanel.addPageTableButtonClickListener(this.searchMerchantProfileTablePageButtonClickListener);
	this.tablePanel.addSizeTableValueChangeListener(this.searchMerchantProfileTableSizeValueChangeListener);
	this.sortingTablePanel.addSortTableButtonClickListener(this.searchMerchantProfileTableSortButtonClickListener);

	this.btnExport.addClickListener(this.searchMerchantProfileExportButtonClickListener);
	this.btnSearch.addClickListener(this.searchMerchantProfileSearchButtonClickListener);
	this.btnNew.addClickListener(this.searchMerchantProfileNewButtonClickListener);
	this.btnClear.addClickListener(this.searchMerchantProfileClearButtonClickListener);
    }

    @Override
    public void initValidator() {
    }

    @SuppressWarnings("rawtypes")
    @Override
    public void initData() {
	try {
	    String hideMerchantTestDefault = this.propertyBAO
		    .getPropertyValue("com.fw.ui.view.profile.SearchMerchantProfileView.default.hideMerchantTest");
	    if (hideMerchantTestDefault != null && !hideMerchantTestDefault.isEmpty()) {
		cbHideMerchantTest.setValue(new Boolean(hideMerchantTestDefault));
	    } else {
		cbHideMerchantTest.setValue(true);
	    }

	    String sortingDefault = this.propertyBAO
		    .getPropertyValue("com.fw.ui.view.profile.SearchMerchantProfileView.default.table.sorting");
	    String sortingItem = this.propertyBAO.getPropertyValue("com.fw.ui.view.profile.SearchMerchantProfileView.table.sorting.item");
	    String sortingCaption = this.messageBAO.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.table.sorting.caption");

	    if (sortingItem != null) {
		String[] sortingItemArr = sortingItem.split(",");
		String[] sortingCaptionArr = sortingCaption.split(",");
		if (sortingItemArr != null) {
		    for (int loop = 0; loop < sortingItemArr.length; loop++) {
			this.sortingTablePanel.addSortingItem(sortingItemArr[loop], sortingCaptionArr[loop]);
		    }
		}
	    }

	    String statusDefault = this.propertyBAO
		    .getPropertyValue("com.fw.ui.view.profile.SearchMerchantProfileView.default.status.item");
	    String statusInclude = this.propertyBAO
		    .getPropertyValue("com.fw.ui.view.profile.SearchMerchantProfileView.include.status.item");
	    String statusExclude = this.propertyBAO
		    .getPropertyValue("com.fw.ui.view.profile.SearchMerchantProfileView.exclude.status.item");
	    String statusIncludeCaption = this.messageBAO
		    .getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.include.status.item.caption");
	    if (statusInclude != null) {
		String[] statusIncludeArr = statusInclude.split(",");
		String[] statusIncludeCaptionArr = statusIncludeCaption.split(",");
		if (statusIncludeArr != null) {
		    for (int loop = 0; loop < statusIncludeArr.length; loop++) {
			this.cbbStatus.addItem(statusIncludeArr[loop]);
			this.cbbStatus.setItemCaption(statusIncludeArr[loop], statusIncludeCaptionArr[loop]);
		    }
		}
	    }
	    List<String> excludes = new ArrayList<String>();
	    if (statusExclude != null) {
		String[] statusExcludeArr = statusExclude.split(",");
		if (statusExcludeArr != null) {
		    for (int loop = 0; loop < statusExcludeArr.length; loop++) {
			excludes.add(statusExcludeArr[loop]);
		    }
		}
	    }
	    List<EntryBean<String, String>> userStatus = this.masterDataBAO.selectAllMerchantProfileStatus();
	    if (userStatus != null) {
		for (EntryBean<String, String> entry : userStatus) {
		    this.statusMap.put(entry.getKey(), entry.getValue());
		    if (!excludes.contains(entry.getKey())) {
			this.cbbStatus.addItem(entry.getKey());
			this.cbbStatus.setItemCaption(entry.getKey(), entry.getValue());
		    }
		}
	    }
	    this.cbbStatus.select(statusDefault);
	    this.cbbStatus.setValue(statusDefault);

	    String typeDefault = this.propertyBAO.getPropertyValue("com.fw.ui.view.profile.SearchMerchantProfileView.default.type.item");
	    String typeInclude = this.propertyBAO.getPropertyValue("com.fw.ui.view.profile.SearchMerchantProfileView.include.type.item");
	    String typeExclude = this.propertyBAO.getPropertyValue("com.fw.ui.view.profile.SearchMerchantProfileView.exclude.type.item");
	    String typeIncludeCaption = this.messageBAO
		    .getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.include.type.item.caption");
	    if (typeInclude != null) {
		String[] statusIncludeArr = typeInclude.split(",");
		String[] statusIncludeCaptionArr = typeIncludeCaption.split(",");
		if (statusIncludeArr != null) {
		    for (int loop = 0; loop < statusIncludeArr.length; loop++) {
			this.cbbMerchantServiceType.addItem(statusIncludeArr[loop]);
			this.cbbMerchantServiceType.setItemCaption(statusIncludeArr[loop], statusIncludeCaptionArr[loop]);
		    }
		}
	    }
	    List<String> typeExcludes = new ArrayList<String>();
	    if (typeExclude != null) {
		String[] statusExcludeArr = typeExclude.split(",");
		if (statusExcludeArr != null) {
		    for (int loop = 0; loop < statusExcludeArr.length; loop++) {
			typeExcludes.add(statusExcludeArr[loop]);
		    }
		}
	    }
	    List<EntryBean<String, String>> servicesType = this.masterDataBAO.selectAllMerchantProfileServiceType();
	    if (servicesType != null) {
		for (EntryBean<String, String> entry : servicesType) {
		    merchantServiceTypeMap.put(entry.getKey(), entry.getValue());
		    if (!typeExcludes.contains(entry.getKey())) {
			this.cbbMerchantServiceType.addItem(entry.getKey());
			this.cbbMerchantServiceType.setItemCaption(entry.getKey(), entry.getValue());
		    }
		}
	    }
	    this.cbbMerchantServiceType.select(typeDefault);
	    this.cbbMerchantServiceType.setValue(typeDefault);

	    List<EntryBean<String, String>> merchantIds = this.masterDataBAO.selectAllMerchantProfileId();
	    if (merchantIds != null) {
		for (EntryBean<String, String> entry : merchantIds) {
		    if (entry.getValue() != null && entry.getValue().trim().length() > 0) {
			this.cbbMerchantId.addItem(entry.getValue());
			this.cbbMerchantId.setItemCaption(entry.getValue(), entry.getValue());
		    }
		}
	    }

	    List<EntryBean<String, String>> companyNames = this.masterDataBAO.selectAllMerchantProfileCompanyName();
	    if (companyNames != null) {
		for (EntryBean<String, String> entry : companyNames) {
		    if (entry.getValue() != null && entry.getValue().trim().length() > 0) {
			this.cbbCompanyName.addItem(entry.getValue());
			this.cbbCompanyName.setItemCaption(entry.getValue(), entry.getValue());
		    }
		}
	    }

	    List<EntryBean<String, String>> urls = this.masterDataBAO.selectAllMerchantProfileURL();
	    if (urls != null) {
		for (EntryBean<String, String> entry : urls) {
		    if (entry.getValue() != null && entry.getValue().trim().length() > 0) {
			this.cbbMerchantUrl.addItem(entry.getValue());
			this.cbbMerchantUrl.setItemCaption(entry.getValue(), entry.getValue());
		    }
		}
	    }

	    List<EntryBean<String, String>> projectContactPersons = this.masterDataBAO.selectAllMerchantProfileProjectContactPerson();
	    if (projectContactPersons != null) {
		for (EntryBean<String, String> entry : projectContactPersons) {
		    if (entry.getValue() != null && entry.getValue().trim().length() > 0) {
			this.cbbProjectContactPerson.addItem(entry.getValue());
			this.cbbProjectContactPerson.setItemCaption(entry.getValue(), entry.getValue());
		    }
		}
	    }

	    if (hideMerchantTestDefault != null && !hideMerchantTestDefault.isEmpty()) {
		searchMerchantProfileBean.setHideMerchantTest(new Boolean(hideMerchantTestDefault));
	    } else {
		searchMerchantProfileBean.setHideMerchantTest(true);
	    }

	    searchCriteriaBean.setDirection1(SearchCriteriaBean.DIRECTION_DESC);
	    searchCriteriaBean.setOrder1(sortingDefault);
	    searchCriteriaBean.setCriteriaBean(searchMerchantProfileBean);
	    searchCriteriaBean.setPage(1);
	    searchCriteriaBean.setSize(30);
	    SearchResultBean<MerchantProfileBean> searchResultBean = merchantProfileBAO.selectMerchantProfile(searchCriteriaBean);

	    Integer totalPage = searchResultBean.getTotalPages();
	    if (totalPage == null) {
		totalPage = 1;
	    }
	    Long totalElement = searchResultBean.getTotalElements();
	    if (totalElement == null) {
		totalElement = 0l;
	    }
	    List<BeanItem> beanItems = this.generateTableItemBean(searchResultBean);

	    this.tablePanel.clearTableData();
	    this.tablePanel.setManualPageTable(1);
	    this.tablePanel.setTableBeanContainerDataTotalPage(beanItems, totalPage, totalElement);

	} catch (Exception e) {
	    e.printStackTrace();
	}

    }

    @SuppressWarnings("rawtypes")
    public List<BeanItem> generateTableItemBean(SearchResultBean<MerchantProfileBean> searchResultBean) {
	List<BeanItem> beanItems = new ArrayList<BeanItem>();
	if (searchResultBean != null && searchResultBean.getContent() != null) {

	    this.tableViewButtons.clear();
	    this.tableEditButtons.clear();
	    this.tableDeleteButtons.clear();

	    DateFormat dateTimeFormatUI = new SimpleDateFormat(StringUtils.PATTERN_DATETIME_UI);

	    String activeStatus = propertyBAO
		    .getPropertyValue("com.fw.ui.view.profile.SearchMerchantProfileView.exclude.status.item.active");

	    for (int loop = 0; loop < searchResultBean.getContent().size(); loop++) {
		MerchantProfileBean merchantProfileBean = searchResultBean.getContent().get(loop);
		SearchMerchantProfileTableItemBean searchMerchantProfileTableItemBean = new SearchMerchantProfileTableItemBean();

		searchMerchantProfileTableItemBean.setId(merchantProfileBean.getId());
		searchMerchantProfileTableItemBean.setMerchantId(merchantProfileBean.getMerchantId());
		searchMerchantProfileTableItemBean.setMerchantName(merchantProfileBean.getMerchantNameEn());
		searchMerchantProfileTableItemBean.setCompanyName(merchantProfileBean.getCompanyNameEn());
		searchMerchantProfileTableItemBean.setMerchantUrl(merchantProfileBean.getUrl());
		searchMerchantProfileTableItemBean.setProjectContactPerson(merchantProfileBean.getContact1Name());

		String serviceType = this.merchantServiceTypeMap.get(merchantProfileBean.getMerchantServiceType());
		if (serviceType == null) {
		    serviceType = merchantProfileBean.getMerchantServiceType();
		}
		searchMerchantProfileTableItemBean.setMerchantServiceType(serviceType);

		String statusText = this.statusMap.get(merchantProfileBean.getMerchantStatus());
		if (statusText == null) {
		    statusText = merchantProfileBean.getMerchantStatus();
		}
		searchMerchantProfileTableItemBean.setStatus(statusText);

		if (merchantProfileBean.getCreateDate() != null) {
		    try {
			String strUI = dateTimeFormatUI.format(merchantProfileBean.getCreateDate());
			searchMerchantProfileTableItemBean.setCreateDateTime(strUI);
		    } catch (Exception e) {
		    }
		}
		if (merchantProfileBean.getLastUpdateDate() != null) {
		    try {
			String strUI = dateTimeFormatUI.format(merchantProfileBean.getLastUpdateDate());
			searchMerchantProfileTableItemBean.setLastUpdateDateTime(strUI);
		    } catch (Exception e) {
		    }
		}

		HorizontalLayout hLayout = new HorizontalLayout();
		Button btnView = new Button(
			this.messageBAO.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.button.table.view"));
		Button btnEdit = new Button(
			this.messageBAO.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.button.table.edit"));
		Button btnDelete = new Button(
			this.messageBAO.getMessage("com.fw.ui.view.profile.SearchMerchantProfileView.button.table.delete"));

		btnView.setData(merchantProfileBean);
		btnEdit.setData(merchantProfileBean);
		btnDelete.setData(merchantProfileBean);

		btnView.addClickListener(this.searchMerchantProfileTableViewButtonClickListener);
		btnEdit.addClickListener(this.searchMerchantProfileTableEditButtonClickListener);
		btnDelete.addClickListener(this.searchMerchantProfileTableDeleteButtonClickListener);

		btnView.setDisableOnClick(true);
		btnEdit.setDisableOnClick(true);
		btnDelete.setDisableOnClick(true);

		btnView.addStyleName("button-green-small");
		btnEdit.addStyleName("button-blue-small");
		btnDelete.addStyleName("button-red-small");

		if (!activeStatus.equals(merchantProfileBean.getMerchantStatus())) {
		    btnDelete.setEnabled(false);
		}

		tableViewButtons.add(btnView);
		tableEditButtons.add(btnEdit);
		tableDeleteButtons.add(btnDelete);

		hLayout.addComponent(btnView);
		hLayout.addComponent(new Label("&nbsp;", ContentMode.HTML));
		hLayout.addComponent(btnEdit);
		hLayout.addComponent(new Label("&nbsp;", ContentMode.HTML));
		hLayout.addComponent(btnDelete);

		searchMerchantProfileTableItemBean.setCommandLayout(hLayout);

		beanItems.add(new BeanItem<SearchMerchantProfileTableItemBean>(searchMerchantProfileTableItemBean));
	    }
	}
	return beanItems;
    }

    public StreamResource getCSVStream(byte[] bytes) {
	FileStreamSource fileStreamSource = new FileStreamSource(bytes);
	String filenamePrefix = propertyBAO.getPropertyValue("com.fw.ui.view.profile.SearchMerchantProfileView.export.filename.prefix");
	String filename = filenamePrefix + new Date().getTime() + ".csv";
	StreamResource resource = new StreamResource(fileStreamSource, filename);
	return resource;
    }

    public MasterDataBAO getMasterDataBAO() {
	return masterDataBAO;
    }

    public MessageBAO getMessageBAO() {
	return messageBAO;
    }

    public PropertyBAO getPropertyBAO() {
	return propertyBAO;
    }

    public MerchantProfileBAO getMerchantProfileBAO() {
	return merchantProfileBAO;
    }

    public Map<String, String> getStatusMap() {
	return statusMap;
    }

    public Map<String, String> getMerchantServiceTypeMap() {
	return merchantServiceTypeMap;
    }

    public SearchMerchantProfileBean getSearchMerchantProfileBean() {
	return searchMerchantProfileBean;
    }

    public SearchCriteriaBean<SearchMerchantProfileBean> getSearchCriteriaBean() {
	return searchCriteriaBean;
    }

    public Panel getSearchMerchantProfilePanel() {
	return searchMerchantProfilePanel;
    }

    public TablePanel getTablePanel() {
	return tablePanel;
    }

    public SortingTablePanel getSortingTablePanel() {
	return sortingTablePanel;
    }

    public Label getLblMerchantId() {
	return lblMerchantId;
    }

    public Label getLblCompanyName() {
	return lblCompanyName;
    }

    public Label getLblMerchantUrl() {
	return lblMerchantUrl;
    }

    public Label getLblProjectContactPerson() {
	return lblProjectContactPerson;
    }

    public Label getLblMerchantServiceType() {
	return lblMerchantServiceType;
    }

    public Label getLblStatus() {
	return lblStatus;
    }

    public ComboBox getCbbMerchantServiceType() {
	return cbbMerchantServiceType;
    }

    public ComboBox getCbbStatus() {
	return cbbStatus;
    }

    public ComboBox getCbbMerchantId() {
	return cbbMerchantId;
    }

    public ComboBox getCbbCompanyName() {
	return cbbCompanyName;
    }

    public ComboBox getCbbMerchantUrl() {
	return cbbMerchantUrl;
    }

    public ComboBox getCbbProjectContactPerson() {
	return cbbProjectContactPerson;
    }

    public CheckBox getCbHideMerchantTest() {
	return cbHideMerchantTest;
    }

    public Button getBtnSearch() {
	return btnSearch;
    }

    public Button getBtnNew() {
	return btnNew;
    }

    public Button getBtnClear() {
	return btnClear;
    }

    public Button getBtnExport() {
	return btnExport;
    }

    public TableHeaderProperty gethNo() {
	return hNo;
    }

    public TableHeaderProperty gethMerchantId() {
	return hMerchantId;
    }

    public TableHeaderProperty gethMerchantName() {
	return hMerchantName;
    }

    public TableHeaderProperty gethCompanyName() {
	return hCompanyName;
    }

    public TableHeaderProperty gethMerchantUrl() {
	return hMerchantUrl;
    }

    public TableHeaderProperty gethMerchantServiceType() {
	return hMerchantServiceType;
    }

    public TableHeaderProperty gethProjectContactPerson() {
	return hProjectContactPerson;
    }

    public TableHeaderProperty gethStatus() {
	return hStatus;
    }

    public TableHeaderProperty gethCreateDate() {
	return hCreateDate;
    }

    public TableHeaderProperty gethLastUpdateDate() {
	return hLastUpdateDate;
    }

    public TableHeaderProperty gethCommand() {
	return hCommand;
    }

    public List<Button> getTableViewButtons() {
	return tableViewButtons;
    }

    public List<Button> getTableEditButtons() {
	return tableEditButtons;
    }

    public List<Button> getTableDeleteButtons() {
	return tableDeleteButtons;
    }

    public void setSearchMerchantProfileBean(SearchMerchantProfileBean searchMerchantProfileBean) {
	this.searchMerchantProfileBean = searchMerchantProfileBean;
    }

    public void setSearchCriteriaBean(SearchCriteriaBean<SearchMerchantProfileBean> searchCriteriaBean) {
	this.searchCriteriaBean = searchCriteriaBean;
    }

}
